package com.ruoyi.system.service.Evaluating.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Evaluating.EvaluationRecord;
import com.ruoyi.system.domain.Evaluating.EvaluationResult;
import com.ruoyi.system.domain.dto.EvaluationRecordSearchDto;
import com.ruoyi.system.mapper.Evaluating.EvaluationRecordMapper;
import com.ruoyi.system.service.Evaluating.IEvaluationRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-04
 */
@Service
public class EvaluationRecordServiceImpl implements IEvaluationRecordService 
{
    @Autowired
    private EvaluationRecordMapper evaluationRecordMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public EvaluationRecord selectEvaluationRecordById(Long id)
    {
        return evaluationRecordMapper.selectEvaluationRecordById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param evaluationRecord 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<EvaluationRecord> selectEvaluationRecordList(EvaluationRecord evaluationRecord)
    {
        return evaluationRecordMapper.selectEvaluationRecordList(evaluationRecord);
    }

    @Override
    public List<EvaluationRecord> searchEvaluationRecordList(EvaluationRecordSearchDto evaluationRecordSearchDto) {
        Long LongId = null;
        String id = evaluationRecordSearchDto.getId();
        String startDate = evaluationRecordSearchDto.getStartDate();
        String endDate = evaluationRecordSearchDto.getEndDate();
        String evaluationName = evaluationRecordSearchDto.getEvaluationName();
        String projectName = evaluationRecordSearchDto.getProjectName();
        if(id!=null) {
            if (StringUtils.isNotBlank(id)) {
                LongId = Long.parseLong(id);
            }
        }
        Date start_date = new Date();
        Date end_date = new Date();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        if(startDate != null ) {
            if (!startDate.trim().isEmpty()) {
                LocalDate localDate = LocalDate.parse(startDate, formatter);
                start_date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
            }
        }

        if (endDate != null && !endDate.trim().isEmpty()) {
            LocalDate localDate = LocalDate.parse(endDate, formatter);
            end_date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        }

        EvaluationRecord evaluationRecord = new EvaluationRecord();
        evaluationRecord.setId(LongId);
        evaluationRecord.setEvaluationName(evaluationName);
        evaluationRecord.setProjectName(projectName);
        System.out.println(evaluationRecord);
        List<EvaluationRecord> evaluationRecords = evaluationRecordMapper.selectEvaluationRecordList(evaluationRecord);
        Iterator<EvaluationRecord> iterator = evaluationRecords.iterator();
        if(startDate!=null&&endDate!=null) {
            if (!startDate.isEmpty() || !endDate.isEmpty()) {
                while (iterator.hasNext()) {
                    EvaluationRecord result = iterator.next();
                    Date evaluateDate = result.getCreateTime();
                    if ((evaluateDate.before(start_date)) || (evaluateDate.after(end_date))) {
                        iterator.remove();
                    }
                }
            }
        }
        return evaluationRecords;

    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param evaluationRecord 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertEvaluationRecord(EvaluationRecord evaluationRecord)
    {
        evaluationRecord.setCreateTime(DateUtils.getNowDate());
        return evaluationRecordMapper.insertEvaluationRecord(evaluationRecord);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param evaluationRecord 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateEvaluationRecord(EvaluationRecord evaluationRecord)
    {
        return evaluationRecordMapper.updateEvaluationRecord(evaluationRecord);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteEvaluationRecordByIds(Long[] ids)
    {
        return evaluationRecordMapper.deleteEvaluationRecordByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteEvaluationRecordById(Long id)
    {
        return evaluationRecordMapper.deleteEvaluationRecordById(id);
    }
}
